package com.tz.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by heziqian on 2016/10/21.
 */
public class DateUtil {
	/**
	 * 根据年月日来构建1个日期对象?
	 */
	public static Date createDate(int year, int month, int date) {
		Calendar cal = Calendar.getInstance();
		cal.set(year, month - 1, date);
		return cal.getTime();
	}

	/**
	 * 将日期对象格式化成字符串
	 * @param d 传入的日期对�?
	 * @param pattern 模版格式
	 */
	public static String format(Date d, String pattern) {
		String sdate = "";
		//参数有效性判�?
		if (null != d && null != pattern) {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			sdate = sdf.format(d);
		}
		return sdate;
	}

	/**
	 * 将字符串解析成Date日期对象
	 * @param sdate 日期的字符串格式
	 * @param pattern 模版格式
	 */
	public static Date parse(String sdate, String pattern) {
		Date d = null;
		//参数有效性判�?
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			if (null != sdate && "" != sdate) {
				d = sdf.parse(sdate);
			}
		} catch (ParseException e) {
			e.printStackTrace();//打印出异常信�?
		}
		return d;
	}
}
